/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "GameEngine.h"

#include "SceneObject.h"
#include "ResourceLoader.h"
#include "NgExecutor.h"


namespace PQEngine
{
	GEngineListener::GEngineListener()
	{
	}

	GEngineListener::~GEngineListener()
	{
	}

	//update scene
	void GEngineListener::update(float deltaT)
	{
	}

	bool GEngineListener::frameRenderingQueued(const Ogre::FrameEvent& evt)
	{
		return true;
	}

	void GEngineListener::setActiveScene(SceneObject* sceneObject)
	{
		if(!sceneObject){
			OGRE_EXCEPT(Ogre::Exception::ERR_INTERNAL_ERROR,
			"Input Scene Object is NULL",
			"GameEngine::setActiveScene");
		}
		SceneObject::setActiveScene(sceneObject);
		_sceneObject=sceneObject;
	}

	const std::string GameEngine::COMMAND_DATA_TYPE="GDATA_V_1";//simple data transfer protocol version 1
	const std::string GameEngine::MAT_TRANSPRENT="TransparentSimpleMaterial";
	const std::string GameEngine::TERRAIN_RESOURCE_GROUP_NAME="TerrainResourceGroup";
	
	const float GameEngine::OBJECT_FRAME_DISTANCE=10.0f;
	
	Ogre::Real GameEngine::_tickRate=  Ogre::Real(60);
	Ogre::Real GameEngine::_animRate= Ogre::Real(25);

	GameEngine::GameEngine(const Ogre::String& homeDir)
		: _rendererType(OGRE_RS_UNKNOWN), _root(0), _isExit(false),_sceneObject(0)
	{
		//init ogre
		_root= new Ogre::Root("plugins_d.cfg");
		if(!_root->restoreConfig()){
			//assert(!"Can't restore!");
			_root->showConfigDialog();
		}
		_root->initialise(false);

		
		
	}

	GameEngine::~GameEngine()
	{
		
	}

	void GameEngine::initResource()
	{
		//SResourceLoader.load();
	}

	void GameEngine::exec(char* data,int len)
	{
		NgExecutor::exec(data,len);
	}

	GameObject* GameEngine::addResource(std::string path)
	{
		if(_sceneObject){
			return _sceneObject->addResource(path);
		}
		return 0;
	}

	GameObject* GameEngine::addGPKResource(std::string path)
	{
		if(_sceneObject){
			return _sceneObject->addGPKResource(path);
		}
		return 0;
	}


	void GameEngine::addCharacterModel(std::string path)
	{
		if(_sceneObject){
			_sceneObject->addCharacterModel(path);
		}
	}
	
	void GameEngine::add2DSprite(Sprite2D* sprite)
	{
		if(_sceneObject){
			_sceneObject->add2DSprite(sprite);
		}
	}

	SceneObject* GameEngine::createSceneObject(Ogre::String hWnd,int width,int height)
	{
		_sceneObject= SceneObject::create(hWnd,width,height);
		
		return _sceneObject;
	}

	void GameEngine::setActiveScene(SceneObject *sceneObject)
	{
		if(!sceneObject){
			OGRE_EXCEPT(Ogre::Exception::ERR_INTERNAL_ERROR,
			"Input Scene Object is NULL",
			"GameEngine::setActiveScene");
		}
		SceneObject::setActiveScene(sceneObject);
		_sceneObject=sceneObject;
	}
	
	GameObject* GameEngine::selectObject(int x,int y)
	{
		if(_sceneObject){
			return _sceneObject->selectObject(x,y);
		}
		return 0;

	}

	GameObject* GameEngine::selectObject(int id)
	{
		if(_sceneObject){
			return _sceneObject->selectObject(id);
		}
		return false;

	}


}

